﻿Imports AutoCAD

Public Class CadUcsServiceImpl
    Implements ICadUcsService
    Public Shared zeroPoint As Double() = New Double() {0, 0, 0}
    Public Shared defaultXVector As Double() = New Double() {1, 0, 0}
    Public Shared defaultYVector As Double() = New Double() {0, 1, 0}
    Public Shared defaultZVector As Double() = New Double() {0, 0, 1}


    '# 三维旋转坐标点
    Function rotatePoint(x As Double, y As Double, z As Double, angleX As Double, angleY As Double, angleZ As Double) As Double() Implements ICadUcsService.RotatePoint
        Dim PI As Double : PI = Math.PI
        ' 将角度转换为弧度
        Dim radX As Double : radX = angleX * PI / 180
        Dim radY As Double : radY = angleY * PI / 180
        Dim radZ As Double : radZ = angleZ * PI / 180
        ' 绕X轴旋转
        Dim y1 As Double : y1 = y * Math.Cos(radX) - z * Math.Sin(radX)
        Dim z1 As Double : z1 = y * Math.Sin(radX) + z * Math.Cos(radX)
        Dim x1 As Double : x1 = x
        ' 绕Y轴旋转
        Dim x2 As Double : x2 = x1 * Math.Cos(radY) + z1 * Math.Sin(radY)
        Dim z2 As Double : z2 = -x1 * Math.Sin(radY) + z1 * Math.Cos(radY)
        Dim y2 As Double : y2 = y1
        ' 绕Z轴旋转
        Dim x3 As Double : x3 = x2 * Math.Cos(radZ) - y2 * Math.Sin(radZ)
        Dim y3 As Double : y3 = x2 * Math.Sin(radZ) + y2 * Math.Cos(radZ)
        Dim z3 As Double : z3 = z2
        Dim arr(2) As Double
        arr(0) = x3
        arr(1) = y3
        arr(2) = z3
        Return arr
    End Function

    Public Function rotatePoint(vector() As Double, angleX As Double, angleY As Double, angleZ As Double) As Double() Implements ICadUcsService.rotatePoint
        Return rotatePoint(vector(0), vector(1), vector(2), angleX, angleY, angleZ)
    End Function




#Region "创建UCS"

    Public Function addUcs(ByRef doc As AcadDocument, ByRef ucsName As String, ByRef XVector() As Double, ByRef YVector() As Double, ByRef Origin() As Double) As AcadUCS Implements ICadUcsService.addUcs
        Return doc.UserCoordinateSystems.Add(Origin, XVector, YVector, ucsName)
    End Function

    Public Function addUcs(ByRef doc As AcadDocument, ByRef ucsName As String, ByRef XVector() As Double, ByRef YVector() As Double) As AcadUCS Implements ICadUcsService.addUcs
        Return addUcs(doc, ucsName, XVector, YVector, zeroPoint)
    End Function

    Public Function addUcs(ByRef doc As AcadDocument, ByRef ucsName As String) As AcadUCS Implements ICadUcsService.addUcs
        Return addUcs(doc, ucsName, defaultXVector, defaultYVector)
    End Function
#End Region






    Public Function rotateUcs(ByRef ucs As AcadUCS, angleX As Double, angleY As Double, angleZ As Double) As AcadUCS Implements ICadUcsService.rotateUcs
        '旋转xy向量
        Dim x() As Double = rotatePoint(ucs.XVector, angleX, angleY, angleZ)
        Dim y() As Double = rotatePoint(ucs.YVector, angleX, angleY, angleZ)
        With ucs
            .XVector = x
            .YVector = y
        End With
        '返回
        Return ucs
    End Function

    Public Function getZeroPoint() As Double() Implements ICadUcsService.getZeroPoint
        Return zeroPoint
    End Function

    Public Function getDefaultXAxis() As Double() Implements ICadUcsService.getDefaultXAxis
        Return defaultXVector
    End Function

    Public Function getDefaultYAxis() As Double() Implements ICadUcsService.getDefaultYAxis
        Return defaultYVector
    End Function

    Public Function getDefaultZAxis() As Double() Implements ICadUcsService.getDefaultZAxis
        Return defaultZVector
    End Function
End Class
